from rest_framework.views import APIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from datetime import date, timedelta

from users.models import User
from goods.models import GoodsVisitCount
from meiduo_admin.serializers.statistical import UserGoodsCountSerializer


class UserCountView(APIView):
    """
    用户总量统计
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1.获取当天日期
        now_date = date.today()
        # 2.获取当天用户总量
        count = User.objects.all().count()
        # 3.返回结果
        return Response({
            'count': count,
            'date': now_date
        })


class UserDayCountView(APIView):
    """
    日增用户统计
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1.获取当天日期
        now_date = date.today()
        # 2.获取当天注册用户总量
        count = User.objects.filter(date_joined__gte=now_date).count()
        # 3.返回结果
        return Response({
            'count': count,
            'date': now_date
        })


class UserActiveCountView(APIView):
    """
    日活用户统计
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1.获取当天日期
        now_date = date.today()
        # 2.获取当天登录过的用户总量
        count = User.objects.filter(last_login__gte=now_date).count()
        # 3.返回结果
        return Response({
            'count': count,
            'date': now_date
        })


class UserOrderCountView(APIView):
    """
    日下单用户统计
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1.获取当天日期
        now_date = date.today()
        # 2.获取当天下单过的用户总量
        count = len(set(User.objects.filter(orders__create_time__gte=now_date)))
        # 3.返回结果
        return Response({
            'count': count,
            'date': now_date
        })


class UserMonthCountView(APIView):
    """
    月增用户统计
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1.获取当天日期
        now_date = date.today()
        # 2.获取一个月前的日期
        begin_date = now_date - timedelta(days=29)
        # 创建空列表保存每天用户量
        date_list = []
        # 循环遍历当天日期
        for i in range(30):
            index_date = begin_date + timedelta(days=i)
            # 下一天日期
            next_date = begin_date + timedelta(days=i+1)
            # 获取当天新增用户量
            count = User.objects.filter(date_joined__gte=index_date, date_joined__lte=next_date).count()
            date_list.append({
                'count': count,
                'date': index_date
            })
        # 3.返回结果
        return Response(date_list)


class UserGoodsCountView(APIView):
    """
    日分类商品访问量
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1.获取当天日期
        now_date = date.today()
        # 2.获取当天访问的商品分类数量信息
        goods = GoodsVisitCount.objects.filter(date__gte=now_date)
        ser = UserGoodsCountSerializer(goods, many=True)
        # 3.返回结果
        return Response(ser.data)
